/* eslint-disable */
import Cookie from "js-cookie";
import { removeAuthorization } from "@/utils/request";
import { TOKEN_STATUS } from "../config/code";
// 401拦截
const resp401 = {
  /**
   * 响应数据之前做点什么
   * @param response 响应对象
   * @param options 应用配置 包含: {router, i18n, store, message}
   * @returns {*}
   */
  onFulfilled(response, options) {
    const {
      data: { code, message: msg }
    } = response;
    if (code === 401) {
      verificationFailure(options, msg);
    }
    return response;
  },
  /**
   * 响应出错时执行
   * @param error 错误对象
   * @param options 应用配置 包含: {router, i18n, store, message}
   * @returns {Promise<never>}
   */
  onRejected(error) {
    return Promise.reject(error);
  }
};

const resp403 = {
  onFulfilled(response, options) {
    const {
      data: { code, message: msg }
    } = response;
    if (code === 403) {
      verificationFailure(options, msg);
    }
    return response;
  },
  onRejected(error) {
    return Promise.reject(error);
  }
};

// token失效拦截
const respTokenInvalid = {
  /**
   * 响应数据之前做点什么
   * @param response 响应对象
   * @param options 应用配置 包含: {router, i18n, store, message}
   * @returns {*}
   */
  onFulfilled(response, options) {
    const { router, message } = options;
    if(response.data.code === -1){
      message.warning(response.data.message);
      return
    }
    if (TOKEN_STATUS.includes(response.data.code)) {
      removeAuthorization();
      message.warning(`${response.data.message}, 请重新登录`);
      router.push("/login");
    }
    return response;
  },
  /**
   * 响应出错时执行
   * @param error 错误对象
   * @param options 应用配置 包含: {router, i18n, store, message}
   * @returns {Promise<never>}
   */
  onRejected(error) {
    return Promise.reject(error);
  }
};

function verificationFailure(options, msg) {
  const { message, router } = options;
  const isLoginPath = !/login/.test(router.history.current.path);
  removeAuthorization();
  if (isLoginPath) {
    router.push("/login").then(() => {
      message.warning(`${msg}, 请重新登录`);
    });
  }
}

const reqCommon = {
  /**
   * 发送请求之前做些什么
   * @param config axios config
   * @param options 应用配置 包含: {router, i18n, store, message}
   * @returns {*}
   */
  onFulfilled(config, options) {
    const { message, router } = options;
    const { url, xsrfCookieName } = config;
    const isLoginPath = !/login/.test(router.history.current.path);
    if (
      url?.indexOf("login") === -1 &&
      xsrfCookieName &&
      !Cookie.get(xsrfCookieName) &&
      isLoginPath
    ) {
      message.warning("认证 token 已过期，请重新登录");
      router.push("/login");
    }
    return config;
  },
  /**
   * 请求出错时做点什么
   * @param error 错误对象
   * @param options 应用配置 包含: {router, i18n, store, message}
   * @returns {Promise<never>}
   */
  onRejected(error, options) {
    const { message } = options;
    message.error(error.message);
    return Promise.reject(error);
  }
};

export default {
  request: [reqCommon], // 请求拦截
  response: [resp401, resp403, respTokenInvalid] // 响应拦截
};
